home *** CD-ROM | disk | FTP | other *** search
- Path: library.erc.clarkson.edu!rpi!not-for-mail
- From: bstowers@pobox.com (Brad Stowers)
- Newsgroups: comp.lang.c++,comp.lang.c++.moderated,comp.std.c++
- Subject: Enumerated type converted to pointer? Legal?
- Date: 8 Mar 1996 04:04:02 -0000
- Organization: NETCOM Network Operations
- Sender: cppmods@netlab.cs.rpi.edu
- Approved: harkness@airmail.net
- Message-ID: <4hobji$mco@netlab.cs.rpi.edu>
- Reply-To: bstowers@pobox.com
- NNTP-Posting-Host: netlab.cs.rpi.edu
- X-Original-Date: Thu, 07 Mar 1996 21:10:37 GMT
-
- #include <stdio.h>
-
- typedef enum { mdType1, mdType2, mdType3 } myType;
-
- void FooBar(int x,
- int* y,
- myType type = mdType1)
- {
- printf("x=%i\ny=%p\ntype=%type\n", x, y, type);
- }
-
- main()
- {
- int x = 10;
- int* y = &x;
- FooBar(x, y, mdType2);
- FooBar(x, mdType1);
- }
-
- ------------------EOF------------------------------
-
- Compile and run the above program. Is it just me, or should the last line
- of the main() function ( FooBar(x, mdType1); ) have caused a type mismatch
- error? I think I know why it didn't: mdType1 evaluates to an integer with
- a value of 0, which is a valid value to pass as a pointer. In support of
- this theory, the line:
-
- FooBar(x, mdType2);
-
- will not compile, but rather generates the expected type mismatch error.
- Obviously, the work-around is simple:
-
- typedef enum { mdType1 = 1, mdType2, mdType3 } myType;
-
- While this will solve the problem, I really think that it is the compiler's
- job to see that mdType1 does not evaluate to the proper type no matter what
- it's internal representation might be.
-
- I have verified that this happens on both Borland and Symantec compilers.
- Can anyone verify it on Watcom and Microsoft for me?
-
-
- Regards,
- Brad
- --------------------------------------------------------
- Brad Stowers | bstowers@pobox.com
- deltaComm Development | Upward, but not Northward.
- Telix for Windows & DOS | -- A Square
- --------------------------------------------------------
- http://www.pobox.com/~bstowers/
-
- [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
- [ Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm ]
- [ Moderation policy: http://www.connobj.com/cpp/guide.htm ]
- [ Comments? mailto:c++-request@netlab.cs.rpi.edu ]
-